package com.achun.ximalayamvvm.view.custom

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import com.achun.ximalayamvvm.utils.LogUtil
import com.google.android.material.imageview.ShapeableImageView

class RotateImageView: ShapeableImageView {
    private var rotatedegree=0f
    private var mNeedRotate=false
    constructor(context: Context, attributeSet: AttributeSet?,defStyle:Int):
            super(context,attributeSet,defStyle){
        //唯一入口
        LogUtil.e(this,"唯一入口执行啊-----")

            }
    constructor(context: Context, attributeSet: AttributeSet?):
           this(context,attributeSet,0){

           }
    constructor(context: Context,):this(context,null){

    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mNeedRotate=true
//        post(object : Runnable {
//            override fun run() {
//                rotatedegree += 5
////                rotatedegree=  if (rotatedegree<=360) rotatedegree else 0f
//                invalidate()//刷新
//                if (mNeedRotate) {
//                    postDelayed(this, 50)
//                }
//            }
//
//        })
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mNeedRotate=false

    }

    override fun onDraw(canvas: Canvas?) {
/*
* 第一个参数是旋转角度
* 第二个参数是旋转的x坐标
* 第三个参数是旋转的y坐标*/
        canvas?.rotate(rotatedegree,width/2f,height/2f)
        super.onDraw(canvas)
    }
    fun setRotate(need:Boolean){
        mNeedRotate=need

    }
}